/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Encryption;

import java.security.*;
import javax.crypto.*;

/**
 * The Crypto class is used by both Galaxia
 * Client and Server to perform encryption
 * decryption and key-pair generation using
 * the member methods.
 * @author sajin
 */
public class Crypto {
	/** 
	 * Generates the KeyPair using the algorithm
	 * and initializes two ciphers for doing the
	 * encryption and decryption.
	 * @param algorithm The algorithm to do the 
	 * encryption and decryption like "RSA"
	 */
	public Crypto(String algorithm) throws NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException {
		KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
		keyPair = keyGen.generateKeyPair();
		encryptionCipher = Cipher.getInstance(algorithm);
		decryptionCipher = Cipher.getInstance(algorithm);
		decryptionCipher.init( Cipher.DECRYPT_MODE, keyPair.getPrivate() );
	}

	/**
	 * Retrieves the public key.
	 * @return PublicKey
	 */
	public PublicKey getPublicKey() {
		return keyPair.getPublic();
	}

	/**
	 * Encrypt the messages using the publicKey
	 * and the algorithm passed to the constructor.
	 * @param message The message to be encrypted.
	 * @param publicKey The public key to be used for encryption.
	 * @return The encrypted message.
	 */
	public byte[] encrypt(byte[] message,byte[] publicKey) {
		return null;
	}

	/**
	 * Decrypts the given message using
	 * the stored private key and the
	 * algorithm passed to the objects constructor.
	 * @param message The message to be encrypted.
	 * @return Decrypted plain text message.
	 */
	public byte[] decrypt(byte[] message ) {
		return null;
	}

	protected KeyPair keyPair;
	protected Cipher encryptionCipher;
	protected Cipher decryptionCipher;

}
